package com.leyou.gateway.filter;

import com.leyou.common.auth.pojo.Payload;
import com.leyou.common.auth.pojo.UserInfo;
import com.leyou.common.auth.utils.JwtUtils;
import com.leyou.common.constants.LyConstants;
import com.leyou.gateway.config.FilterProperties;
import com.leyou.gateway.config.JwtProperties;
import com.leyou.gateway.scheduler.AppTokenScheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @date: 2021/4/10 0010 17:34
 * @description: 用户统一鉴权
 */
@Component
public class AuthFilter implements GlobalFilter, Ordered {
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private FilterProperties filterProperties;
    @Autowired
    private AppTokenScheduler appTokenScheduler;


    /**
     * 编写过滤逻辑
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        //把申请的token添加到请求头中
        request.mutate().header(LyConstants.APP_TOKEN_HEADER,appTokenScheduler.getAppToken());

        //加入拦截白名单逻辑
        //取出白名单路径
        List<String> allowPaths = filterProperties.getAllowPaths();
        //获取当前访问路径
        String curPath = request.getURI().getPath();
        //判断当前服务是否在白名单
        for (String allowPath : allowPaths) {
            if (curPath.contains(allowPath)) {
                //放行到微服务
                return chain.filter(exchange);
            }
        }

        String token = null;
        try {
            //1.从请求中取出token
            token = request.getCookies().getFirst(jwtProperties.getCookie().getCookieName()).getValue();
        } catch (Exception e) {
            //拒绝访问
            //返回401的状态码
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            //中止访问
            return response.setComplete();

        }


        Payload<UserInfo> payload = null;
        //2.判断token是否合法
        try {
            payload = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey(), UserInfo.class);
        } catch (Exception e) {
            //拒绝访问
            //返回401的状态码
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            //中止访问
            return response.setComplete();
        }

        return chain.filter(exchange);
    }


    /**
     * 返回过滤器执行顺序值
     * 数值越大，优先级越低
     * @return
     */
    @Override
    public int getOrder() {
        return 1;
    }
}
